<template>
  <div class="container">
    <!--<div class="comments">13评论</div>-->
    <div class="floor qa-wrapper">
      <div v-if="netlsData.length > 0">
      <van-list
        v-model="loading"
        :finished="finished"
        :immediate-check="false"
        finished-text="没有更多了"
        @load="onLoad"
      >
      <div class="qa-item" v-for="item in netlsData">
        <div class="thumb">
          <img v-if="!item.pic" class="user-thumb" src="@/assets/images/userimg.png"/>
          <img  class="floor-user-thumb" v-else :src="item.pic"/>
        </div>
        <div class="qa-con">
          <h2 class="qa-title">{{item.title}}</h2>
          <div class="qa-content">{{item.content}}</div>
          <div class="qa-info"><span>{{item.posttime}}</span> | <span>提问人：{{item.poster}}</span></div>
          <div class="qa-reply"><span>{{item.tcname}}老师回复：</span><p>{{item.answer}}</p></div>
        </div>
      </div>
      </van-list>
    </div>
      <div v-else class="nodata">--- 暂无数据 ---</div>
    </div>
    <div class="btn-wrapper">
      <button class="exam-btn" @click="editorComment">发表问题</button>
    </div>
    <van-dialog
      v-model="showComment"
      title="发表问题"
      :beforeClose="beforeClose"
      show-cancel-button
      confirm-button-text="发表"
    >
      <div class="container">
          <van-field
            readonly
            clickable
            name="picker"
            :value="valueDepartment"
            label="部门"
            placeholder="点击选择"
            @click="showPicker = true"
          />

          <van-field
            v-model="title"
            name="title"
            label="标题"
            placeholder="请填写标题"
            :rules="[{ required: true, message: '请填写标题' }]"
          />

          <van-field
            v-model="content"
            name="content"
            rows="3"
            autosize
            label=""
            type="textarea"
            placeholder="请输入内容"
          />
      </div>
    </van-dialog>
    <van-popup v-model="showPicker" position="bottom">
      <van-picker
        show-toolbar
        :columns="columns"
        @confirm="onConfirm"
        @cancel="showPicker = false"
      />
    </van-popup>

  </div>
</template>

<script>
  import util from '@/common/util';
  export default {
    data () {
      return {
        showComment: false,
        id: '',
        pic:'',
        stname: '',
        page: 1,
        baseURL: '',
        netlsData: [],
        valueDepartment: '',
        tchid: '',
        columns: [],
        showPicker: false,
        title: '',
        content: '',
        loading: false,
        finished: false,
      }
    },
    created() {
      if(util.storage.get('userinfo')){
        this.id = util.storage.get('userinfo').id; // 获取缓存学员ID
        this.pic = util.storage.get('userinfo').pic;
        this.stname = util.storage.get('userinfo').stname;
      }
      this.baseURL = util.baseURL;
      console.log(this.id)
      this.getDetail();
    },
    methods: {
      onConfirm(value) { // 确认选择
        this.valueDepartment = value['text'];
        this.tchid = value['id'];
        this.showPicker = false;
      },
      getDetail() {
        let that = this;
        that.$toast.loading({
          message: '加载中...'
        });
        that.$api.post('/mobile/Stmsg/wsdy',{"user":{"id": that.id, "page":that.page}}, r => {
          console.log('答疑内容', r)
          r.data.teacher.forEach(function(item){  //获取单位列表
            let teacherObj = {}
            teacherObj['text'] = item.tcname;
            teacherObj['id'] = item.id;
            that.columns.push(teacherObj)
          });
          let rows = r.data.netls; //请求返回当页的列表
          console.log('rows',rows);
          that.$toast.clear();
          that.loading = false;
          if (rows == null || rows.length === 0) {
            this.finished = true; // 加载结束
            return;
          }
          that.netlsData = that.netlsData.concat(rows);
        })
      },
      onLoad() { //下拉加载更多
        this.page++;
        this.getDetail();
      },
      editorComment() {
        let that = this;
        that.showComment = true;
      },
      beforeClose(action, done) {
        let that = this;
        if(action === 'confirm'){
          if(!that.title){
            that.$toast('标题不能为空！')
            done(false)
            return false;
          }else if (!that.content) {
            that.$toast('内容不能为空！')
            done(false)
            return false;
          }else if (!that.valueDepartment) {
            that.$toast('部门不能为空！')
            done(false)
            return false;
          }
          let formData = {
            tchid:that.tchid,
            stuid: that.id,
            title: that.title,
            pic: that.pic,
            poster: that.stname,
            posttime: parseInt(new Date().getTime() / 1000),
            content: that.content
          };
          console.log('formData',formData);
          that.$api.post('/mobile/Stmsg/fbwt', {'user':formData}, r => {
            console.log('提问成功',r)
            if(r.code == 1){
              setTimeout(done, 500);
              this.$router.replace({
                path: '/refresh',
                query: {
                  id: r.data.hfid,
                  t: Date.now()
                }
              })
            }
          });

        } else if(action === 'cancel') {
          done() //关闭
        }
      }
    }
  }
</script>

<style scoped>
  .floor {
    padding-bottom: 60px;
  }
  .forum-title {
    padding: 12px;
    font-size: .34rem;
    background-color: #fff;
  }
  .forum-floor {
    margin-top: 10px;
    background-color: #fff;
  }
  .forum-floor-top, .forum-floor-bottom {
    display: flex;
    padding: .3rem .2rem;
  }
  .floor-user-thumb {
    width: 35px;
    height: 35px;
    border-radius: 50%;
  }
  .forum-floor-topright {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding-left: 10px;
    line-height: 1.5;
  }
  .comments {
    padding: 12px;
    font-size: .3rem;
    background-color: #fff;
  }
  .user-name {
    font-size: .32rem;
  }
  .forum-floor-bottom {
    font-size: .28rem;
    border-top: 1px solid #e8ebf2;
    line-height: 1.5;
  }
  .forum-floor:last-child {
    margin-bottom: 60px;
  }
  .send-msg {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
  }

</style>
